//
//  PasscodeViewController.m
//  KurtulusLaw
//
//  Created by phuc phuc on 12/2/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PasscodeViewController.h"
#import "RePasscodeViewController.h"
#import "CCommon.h"
#import "TermAndCondition.h"
#import "FrontViewController.h"
#import "RearViewController.h"
#import "KurtulusLawController.h"
#import "LoginViewController.h"
@interface PasscodeViewController()
{
	int temp;
}
@end

@implementation PasscodeViewController
@synthesize isChangePasscode,isCheckPasscode;
@synthesize lbMessage;
@synthesize text1;
@synthesize text2;
@synthesize text3;
@synthesize text4;
@synthesize lb1;

#pragma mark - process passcode
- (void)processPass:(id)sender {
    RePasscodeViewController *repass = [[RePasscodeViewController alloc] init];
    repass.number1 = self.text1.text;
    repass.number2 =  self.text2.text;
    repass.number3 = self.text3.text;
    repass.number4 = self.text4.text;
    [self.navigationController pushViewController:repass animated:YES];
}
-(BOOL) checkPasscodeRightOrWrong
{
	NSUserDefaults *userDefauls = [NSUserDefaults standardUserDefaults];
    NSString *code1 = [userDefauls objectForKey: kCode1];
    NSString *code2 = [userDefauls objectForKey: kCode2];
    NSString *code3 = [userDefauls objectForKey: kCode3];
    NSString *code4 = [userDefauls objectForKey: kCode4];
    if ([code1 isEqualToString: self.text1.text]&&[code2 isEqualToString: self.text2.text]&&[code3 isEqualToString: self.text3.text]&&[code4 isEqualToString: self.text4.text])
		return YES;
	else
		return NO;
}

-(void)turnOffPasscode
{
	if ([self checkPasscodeRightOrWrong]) {
		[CCommon resetPasscode];
        self.lbMessage.hidden = YES;
        [self dismissModalViewControllerAnimated:YES];
	}
    else
    {
        self.text1.text = @"";
        self.text2.text = @"";
        self.text3.text = @"";
        self.text4.text = @"";
        [self.text1 becomeFirstResponder];
        self.lbMessage.hidden = NO;
    }
}
- (void)openHomePage:(id)sender {
	FrontViewController *frontViewController = [[FrontViewController alloc] init];
	RearViewController *rearViewController = [[RearViewController alloc] init];
	
	UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
	
	KurtulusLawController *revealController = [[KurtulusLawController alloc] initWithFrontViewController:navigationController rearViewController:rearViewController];
	UIViewController *view = revealController;
	
	[view setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
	[self presentModalViewController:view animated:YES];
	[revealController revealToggle:nil];
	
}
-(IBAction)textChanged : (id)sender
{
    UITextField *text = (UITextField*)sender;
    if (text.tag == 1) {
        [self.text2 becomeFirstResponder];
    }
    else if (text.tag == 2)
    {
        [self.text3 becomeFirstResponder];
    }
    else if (text.tag == 3)
    {
        [self.text4 becomeFirstResponder];
    }
    else if (text.tag == 4)
    {
        [self.text4 resignFirstResponder];
		if (self.isCheckPasscode) {
			//
			if ([self checkPasscodeRightOrWrong])
			{
				// if check passcode wrong > 3 times, logout
				NSUserDefaults *userDefauls = [NSUserDefaults standardUserDefaults];
				BOOL checkTerm = [[userDefauls objectForKey:@"checkterm"] boolValue];
				temp = 1;
				if (checkTerm) {
					[self openHomePage:self];
				}
				else
				{
					TermAndCondition *term = [[TermAndCondition alloc] init];
					[self presentModalViewController:term animated:NO];
				}
				
				
			}
			else
			{
				self.text1.text = @"";
				self.text2.text = @"";
				self.text3.text = @"";
				self.text4.text = @"";
				[self.text1 becomeFirstResponder];
				self.lbMessage.hidden = NO;
				temp++;
				if (temp > 3) {
					[CCommon resetDataWhenLogout];
					[self dismissModalViewControllerAnimated:NO];
				}
			}
			
		}
		else
		{
			if (!self.isChangePasscode) {
				if ([CCommon checkPasscode]) {
					[self turnOffPasscode];
				}
				else
					[self processPass:self];
			}
			else
			{
				[self processPass:self];
			}
		}
        
    }
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = NSLocalizedString(@"Passcode", nil);
	self.lbMessage.text = NSLocalizedString(@"Wrong Passcode", nil);
	self.lb1.text = NSLocalizedString(@"Enter your Passcode", nil);
	
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgback.png"]];
    [self.text1 becomeFirstResponder];
	
	UIButton *btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 67, 24)];
	[btnCancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[btnCancel setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
	btnCancel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"logout-67x24.png"]];
	btnCancel.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
	btnCancel.titleLabel.textColor = [UIColor grayColor];
	[btnCancel addTarget:self action:@selector(cancelPasscode:) forControlEvents:UIControlEventTouchUpInside];
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCancel];
	
	[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav-bg-black"] forBarMetrics:UIBarMetricsDefault] ;
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationController.navigationItem.hidesBackButton = YES;
    self.lbMessage.hidden = YES;
	temp = 1;
}
-(void)cancelPasscode : (id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.text1.text = @"";
    self.text2.text = @"";
    self.text3.text = @"";
    self.text4.text = @"";
    [self.text1 becomeFirstResponder];
    
}
- (void)viewDidUnload
{
    [self setText1:nil];
    [self setText2:nil];
    [self setText3:nil];
    [self setText4:nil];
    [self setLbMessage:nil];
    [self setText1:nil];
    [self setText2:nil];
    [self setText3:nil];
    [self setText4:nil];
    [self setLb1:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end
